package com.tiger.utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;



public class Utils {
    public static Map<String, Object> entityFieldToValues(Object obj) {
        Map<String, Object> result = new HashMap<>();
        Class<?> clazz = obj.getClass();

        while (clazz != Object.class) { // 终止条件为Object类，避免无限循环
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true); // 允许访问私有字段
                try {
                    result.put(field.getName(), field.get(obj));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            clazz = clazz.getSuperclass(); // 递归检查父类
        }

        return result;
    }



    public static String maskMobileNumber(String number) {
        if (number == null || number.length() < 8) {
            return number; // 如果手机号码格式不正确，直接返回原手机号码
        }
        int beginIndex = 3; // 保留前3位
        int endIndex = number.length() - 4; // 保留后4位
        return number.substring(0, beginIndex) + "****" + number.substring(endIndex);
    }

    public static String maskName(String name) {
        int len = name.length();
        char firstChar = name.charAt(0);



        if(len <= 1) return firstChar + "";

        String flag = "*";

        if(len == 2) return firstChar + flag;

        // 获取最后一个字符
        char lastChar = name.charAt(len - 1);

        if(len == 3) return firstChar + flag + lastChar;

        return firstChar + (flag.repeat(len - 2)) + lastChar;


    }


    public static String maskUserAccount(String account) {
        int len = account.length();

        String flag = "*";

        if(len <= 5) {
            char firstChar = account.charAt(0);
            char lastChar = account.charAt(len - 1);
            return firstChar + (flag.repeat(len - 2)) + lastChar;
        }
//        if(len <= 8) {
//            // 获取前两位字符
//            String firstChar = account.substring(0, 2);
//            // 获取后两位字符
//            String lastChar = account.substring(account.length() - 3);
//            return firstChar + (flag.repeat(len - 5)) + lastChar;
//        }



        String firstChar = account.substring(0, 2);
        // 获取后两位字符
        String lastChar = account.substring(account.length() - 3);
        return firstChar + (flag.repeat(len - 5)) + lastChar;
    }

    public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    public static void copyPropertiesIgnoreNull(Object src, Object target, String... ignoreProperties){
        BeanUtils.copyProperties(src, target, Stream.of(getNullPropertyNames(src), ignoreProperties).flatMap(Stream::of).toArray(String[]::new));
    }

}
